﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2010
// admin@franknagl.de
//
namespace SBIP.OptionsForms
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Windows.Forms;
    using Filter;
    using Helper;

    /// <summary>Filter specified properties form.</summary>
    public partial class VoronoiDiagramForm : Form
    {
        private readonly Bitmap bitmap;
        private readonly Panel panel;
        private readonly Processor processor;
        /// <summary>
        /// Factor for multiplying cursor's X-coordinate at <see cref="panel"/> 
        /// to get correct image pixel coordinate.
        /// </summary>
        private readonly float scaleCursorX;
        /// <summary>
        /// Factor for multiplying cursor's Y-coordinate at <see cref="panel"/> 
        /// to get correct image pixel coordinate.
        /// </summary>
        private readonly float scaleCursorY;
        private readonly List<Point> points;

        /// <summary>
        /// Initializes a new instance of the <see cref="VoronoiDiagramForm"/> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        /// <param name="panel">The render panel.</param>
        /// <param name="scaleCursorX">The scale cursor X.</param>
        /// <param name="scaleCursorY">The scale cursor Y.</param>
        public VoronoiDiagramForm(
            Processor processor, 
            Panel panel, 
            float scaleCursorX, 
            float scaleCursorY)
        {
            InitializeComponent();
            this.panel = panel;
            this.scaleCursorX = scaleCursorX;
            this.scaleCursorY = scaleCursorY;
            this.processor = processor;
            processor.Filter = new Original();
            bitmap = new Bitmap(
                processor.OriginalBitmap.Width, 
                processor.OriginalBitmap.Height, 
                PixelFormat.Format24bppRgb);
            points = new List<Point>();
            processor.ChangeTexture(bitmap);

            // panel.MouseMove += PanelMouseMove;
            panel.MouseClick += PanelMouseClick;
        }

        private void PanelMouseClick(object sender, MouseEventArgs e)
        {
            int mouseX = panel.PointToClient(Cursor.Position).X;
            int mouseY = panel.PointToClient(Cursor.Position).Y;

            int x = (int)Math.Round(mouseX * scaleCursorX + 0.5f);
            int y = (int)Math.Round(mouseY * scaleCursorY + 0.5f);
            Point p = new Point(x, y);
            points.Add(p);
            Color c = GetColor((byte)points.Count);
       
            Drawing24Or32Bpp.DrawCross(bitmap, p, c, 3, 7);
            processor.ChangeTexture(bitmap);

            lbPointNumbers.Text = points.Count.ToString();
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {
            // processor.ChangeTexture(b);
            processor.Filter = new VoronoiDiagram();
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            BitmapHelper.ResetBitmap(bitmap);
            processor.Filter = new Original();
            processor.ChangeTexture(processor.OriginalBitmap);
            points.Clear();
            lbPointNumbers.Text = points.Count.ToString();
        }

        private static Color GetColor(byte number)
        {
            int m = 0, n = 0; // 7 interval, 49 interval
            byte r = 0, g = 0, b = 0;

            for (byte i = 0; i < 255; i++, m++, n++, r += 42)
            {
                if (m == 7 && n != 49)
                {
                    g += 42;
                    r = 0;
                    m = 0;
                }

                if (n == 49)
                {
                    b += 42;
                    r = g = 0;
                    m = n = 0;
                }

                if (i == number)
                {
                    break;
                }                
            }

            return Color.FromArgb(r, g, b);
        }
    }
}
